#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time     : 2018/7/2 19:28
# @Author   : BYF
# file      : addcoupon_page.py
# @version  :


from base_page import BasePage
from common import function
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
import time

class AddCouponPage(BasePage):

    #Action
    iframe_loc = (By.CSS_SELECTOR ,"#iframe1")
    coupon_rule_name_loc = (By.CSS_SELECTOR, "#coupon_rule_name")
    coupon_money_loc = (By.CSS_SELECTOR, "#coupon_money")
    finance_type_loc = (By.CSS_SELECTOR, "select[name='finance_type']")
    service_code_loc = (By.CSS_SELECTOR, "select[name='service_code']")
    cost_configuration_loc = (By.CSS_SELECTOR, "select[name='cost_configuration_id']")
    indate_end_loc = (By.CSS_SELECTOR, "input[name='indate_end']")
    tenant_phone_loc = (By.CSS_SELECTOR, "#tenant_phone")
    remarks_loc = (By.CSS_SELECTOR, "textarea[name='remarks']")
    save_loc = (By.CSS_SELECTOR, "button[id='btn-ckin']")


    def _open(self):
        base_url = self.config.get("url", "base_url")
        addcoupon_url = self.config.get("url", "addcoupon_url")
        self.open(base_url)
        function.add_cookies(self.driver)
        self.open(base_url + addcoupon_url)
        self.switch_to_iframe(self.iframe_loc)


    def add_coupon(self):

        coupon_rule_name = u"这是自动添加的优惠券规则"
        coupon_money = "500"
        finance_type = u"无责优惠"
        service_code = u"入住"
        # cost_configuration_id = u"租金" #租金
        cost_configuration_id = u"服务费" #服务费
        end_time = "2018-08-08"
        tenant_phone = "18080885489"
        remarks = u"这个是自动添加的优惠券，自己写的备注信息哟~！"


        self._open()
        self.send_keys(self.coupon_rule_name_loc,coupon_rule_name) #输入优惠券规则名称
        self.send_keys(self.coupon_money_loc, coupon_money) #输入优惠券金额
        finance_type_list = Select(self.driver.find_element(*self.finance_type_loc))
        finance_type_list.select_by_visible_text(finance_type) #选择财务类型
        service_code_list = Select(self.driver.find_element(*self.service_code_loc))
        service_code_list.select_by_visible_text(service_code) #选择业务场景
        cost_configuration_list = Select(self.driver.find_element(*self.cost_configuration_loc))
        cost_configuration_list.select_by_visible_text(cost_configuration_id) #选择优惠科目
        js = "$('input[name=indate_end]').removeAttr('readonly')"  # js去掉begin_time的readonly属性
        self.driver.execute_script(js)
        self.send_keys(self.indate_end_loc, end_time) #输入结束日期
        self.send_keys(self.tenant_phone_loc, tenant_phone) #输入租客电话
        self.send_keys(self.remarks_loc ,remarks) #输入备注信息
        self.driver.find_element(*self.save_loc).click()